Cypress and MSAL SSO

Cypress and MSAL SSO

Created an application that used [[Azure SSO Integration]] and wanted to test with Cypress. I was thinking about bypassing the login altogether but the doucmentation advised against this.

Instead of skipping authentication or manually typing in a username and password each time, you should inject the relevant information into sessionStorage or localStorage.

Requirements:

  1. Azure AD Tenant
    1. Worth creating a seperate one for testing environment
  2. Create the API / App applications as described in [[Azure SSO Integration]]

Cypress Setup

First you'll need to store some secrets in Cypress to be used when getting auth details:

WARNING You should not check in the test users clientSecret or password into VCS. See [[Azure Key Vault JavaScript]] for a way to inject these variables, or share them in some other secure way.

Along with the following support file:

This file is responsible for making a POST request to Azure AD and setting the session storage with your credentials.

We can now add the exposed login function to Cypress:

You should also add a type:

Now you can use the login command to skip your splash screen and authenticated a user! 🎉

Example usage:

Cypress and MSAL SSO